/*
 * @Date: 2024-02-26 11:08:35
 * @LastEditors: lyx
 * @LastEditTime: 2024-03-14 14:45:02
 * @FilePath: \taro-template3\src\components\CustomNav\index.tsx
 * @Description:
 */
import { useLaunch } from "@tarojs/taro";
import { useGlobalData } from "@/store/app";
import { ScrollView, View } from "@tarojs/components";
import { Add, User } from "@nutui/icons-react-taro";
import {
  ActionSheet,
  Avatar,
  Badge,
  Button,
  Cell,
  Dialog,
  Menu,
  Swipe,
  TextArea,
  Toast,
} from "@nutui/nutui-react-taro";
import { FC, useState } from "react";
import { goToPage } from "@/utils/common";
import ChatTalk from "../ChatTalk";
import { fetchUpdateSession } from "@/pages/index/services";
import "./index.scss";

type HandleTalk = "add" | "select" | "editor" | "del";

interface CustomNav {
  selectTalk: any;
  talkLists: any[];
  onHandleChangeTalk: (
    type: HandleTalk,
    selectTalk?: any,
    cb?: (info?: any) => void
  ) => void;
  updateTask: (task: any) => void;
}

function CustomNav(props: CustomNav) {
  const { systemInfo, useInfo } = useGlobalData();

  const {
    selectTalk = {},
    talkLists = [],
    updateTask,
    onHandleChangeTalk,
  } = props;

  const [visible, setVisible] = useState(false);
  const [dialogVisible, setDialogVisible] = useState(false);
  const [currentTask, setCurrentTask] = useState<any>({});

  const onHandleTalk = (type: HandleTalk, selectTalk?: any) => {
    setVisible(false);
    onHandleChangeTalk(type, selectTalk);
  };

  useLaunch(() => {});

  const editTask = (item) => {
    setCurrentTask(item);
    setDialogVisible(true);
  };

  const updateSession = async () => {
    const dto = {
      id: currentTask.id,
      summary: currentTask.content || "新对话",
    };
    const { success, msg } = await fetchUpdateSession(dto);
    if (success) {
      const updateItem = {
        id: dto.id,
        content: dto.summary,
      };
      updateTask(updateItem);
      setDialogVisible(false);
    } else {
      return Toast.show("chat-toast", { title: msg });
    }
  };

  return (
    <View
      style={{
        marginTop: `${systemInfo?.statusBarHeight}px`,
        height: `${systemInfo?.navbarHeight}px`,
      }}
      className="custom_nav"
    >
      {/* 头像部分 */}
      <View className="custom_nav_left">
        <Badge value={8}>
          <Avatar
            className="custom_nav_left_avatar"
            src={`${useInfo?.avatarUrl || ""}`}
            shape="round"
            onClick={() => goToPage("/packageA/pages/personal/index")}
          />
        </Badge>
        <Add
          className="custom_nav_left_add_talk"
          size="20px"
          onClick={() => onHandleChangeTalk("add")}
        />
      </View>
      {/* 当先处在的对话 */}
      <View
        className="custom_nav_center"
        onClick={() => {
          setVisible(!visible);
        }}
      >
        {selectTalk?.content || "新对话"}
      </View>
      <View className="custom_nav_right"></View>
      <ActionSheet
        className="custom_nav_action_sheet"
        visible={visible}
        onSelect={() => {
          setVisible(false);
        }}
        onCancel={() => setVisible(false)}
      >
        <ChatTalk
          talkLists={talkLists}
          selectTalk={selectTalk}
          onHandleChangeTalk={onHandleTalk}
        />
      </ActionSheet>
    </View>
  );
}

export default CustomNav;
